package com.banban.user.controller;

import com.banban.user.dto.*;
import com.banban.user.exception.BusinessException;
import com.banban.user.service.UserService;
import com.banban.user.vo.ConversationSelectVO;
import com.banban.user.vo.DataResult;
import com.banban.user.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@RestController
@Api(tags = "用户登录模块")
@RequestMapping("/user")
@Slf4j
public class UserLoginController {

    @Resource
    private UserService userService;

    /**
     * 获取短信验证码
     */
    @PostMapping("/getCodeVerify")
    public DataResult getCodeVerify(@RequestBody CodeVerifyLoginDTO dto) throws Exception {
        log.info("获取短信验证码，入参:{}",dto);
        if (StringUtils.isEmpty(dto.getPhone())){
            throw new BusinessException("手机号码不能为空");
        }
        Integer result = userService.getCodeVerify(dto.getPhone());
        return DataResult.success();
    }

    /**
     * nfc登录注册下发验证码
     */
    @PostMapping("/nfcLoginGetCode")
    public DataResult nfcLoginGetCode(@RequestBody CodeVerifyLoginDTO dto) throws Exception {
        log.info("nfc登录注册下发验证码，入参:{}",dto);
        if (StringUtils.isEmpty(dto.getPhone())){
            throw new BusinessException("手机号码不能为空");
        }
        userService.nfcLoginGetCode(dto.getPhone());
        return DataResult.success();
    }

    /**
     * 短信验证码登录
     */
    @PostMapping("/verifyCodeLogin")
    public DataResult verifyCodeLogin(@RequestBody CodeVerifyLoginDTO dto) {
        log.info("短信验证码登录，入参:{}",dto);
        if (StringUtils.isEmpty(dto.getPhone()) || StringUtils.isEmpty(dto.getRandomCode())){
            throw new BusinessException("手机号码或验证码不能为空");
        }
        Integer result = userService.verifyCodeLogin(dto);
        return DataResult.success(result);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/getUserInfo")
    public DataResult getUserInfo() {
        log.info("====获取用户信息====");
        UserInfoVO vo = userService.getUserInfo();
        log.info("获取用户信息出参：{}",vo);
        return DataResult.success(vo);
    }

    /**
     * 获取默认图片地址
     */
    @GetMapping("/getDefaultPicUrl")
    public DataResult getDefaultPicUrl() {
        String url = userService.getDefaultPicUrl();
        log.info("获取默认图片地址：{}",url);
        return DataResult.success(url);
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册接口")
    public DataResult register(@RequestBody @Valid RegisterDTO dto) {
        log.info("用户注册接口：{}",dto);
        userService.registerNew(dto);
        return DataResult.success();
    }

    @PostMapping("/registerAndLogin")
    @ApiOperation(value = "注册并登录")
    public DataResult registerAndLogin(@RequestBody @Valid RegisterDTO dto) {
        log.info("注册并登录接口：{}",dto);
        userService.registerAndLogin(dto);
        return DataResult.success();
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation(value = "编辑个人信息接口")
    public DataResult updateUserInfo(@RequestBody UpdateUserDTO dto) {
        log.info("编辑个人信息接口：{}",dto);
        userService.updateUserInfo(dto);
        return DataResult.success();
    }

    /**
     * 上传头像
     */
    @PostMapping("/uploadPic")
    public DataResult uploadPic(@RequestParam("file") MultipartFile file) {
        if (!Objects.nonNull(file) || file.isEmpty()) {
            throw new BusinessException("文件不能为空");
        }
        String url = userService.uploadPic(file);
        return DataResult.success(url);
    }

    /**
     * 绑定NFC
     */
    @GetMapping("/bindNfc/{nfcId}")
    public DataResult bindNfc(@PathVariable("nfcId") String nfcId) {
        log.info("绑定NFC：{}",nfcId);
        if (StringUtils.isEmpty(nfcId)) {
            throw new BusinessException("nfcId不能为空");
        }
        userService.bindNfc(nfcId);
        return DataResult.success();
    }

    /**
     * 解绑NFC
     */
    @PostMapping("/notBindNfc")
    public DataResult notBindNfc(@RequestBody @Valid NfcLoginDTO dto) {
        log.info("解绑NFC");
        userService.notBindNfc(dto.getNfcId());
        return DataResult.success();
    }

    /**
     * 登出
     */
    @GetMapping("/logout")
    public DataResult logout() {
        log.info("登出");
        userService.logout();
        return DataResult.success();
    }

    /**
     * NFC登录
     */
    @PostMapping("/nfcLogin")
    public DataResult nfcLogin(@RequestBody @Valid NfcLoginDTO dto) {
        log.info("nfcLogin 入参：{}",dto);
        Integer result =  userService.nfcLogin(dto.getNfcId());
        return DataResult.success(result);
    }

    /**
     * 换绑手机号下发验证码
     */
    @PostMapping("/notBindPhoneGetCode")
    public DataResult notBindPhoneGetCode(@RequestBody CodeVerifyLoginDTO dto) throws Exception {
        log.info("换绑手机号下发验证码 入参：{}",dto);
        if (StringUtils.isBlank(dto.getPhone())){
            throw new BusinessException("手机号不能为空");
        }
        userService.notBindPhoneGetCode(dto.getPhone());
        return DataResult.success();
    }

    /**
     * 更换新手机号下发验证码
     */
    @PostMapping("/updatePhoneGetCode")
    public DataResult updatePhoneGetCode(@RequestBody CodeVerifyLoginDTO dto) throws Exception {
        log.info("更换新手机号下发验证码 入参：{}",dto);
        if (StringUtils.isBlank(dto.getPhone())){
            throw new BusinessException("手机号不能为空");
        }
        userService.updatePhoneGetCode(dto.getPhone());
        return DataResult.success();
    }

    /**
     * 换绑手机号
     */
    @PostMapping("/updatePhone")
    public DataResult updatePhone(@RequestBody @Valid UpdatePhoneDTO dto) {
        log.info("换绑手机号 入参：{}",dto);
        userService.updatePhone(dto);
        return DataResult.success();
    }

    /**
     * 获取聊天记录
     */
    @GetMapping("/getHistory")
    public DataResult getHistory() {
        log.info("获取聊天记录");
        List<ConversationSelectVO> vos = userService.getHistory();
        return DataResult.success(vos);
    }

    /**
     * 获取聊天日期信息
     */
    @GetMapping("/getHistoryDate")
    public DataResult getHistoryDate() {
        log.info("获取聊天日期信息");
        List<String> vos = userService.getHistoryDate();
        return DataResult.success(vos);
    }

    /**
     * 根据日期获取聊天记录
     */
    @PostMapping("/getHistoryByDate")
    public DataResult getHistoryByDate(@RequestBody @Valid GetHistoryByDateDTO dto) {
        log.info("根据日期获取聊天记录");
        List<ConversationSelectVO> vos = userService.getHistoryByDate(dto);
        return DataResult.success(vos);
    }

}